var fs = require('fs')
const axios = require('axios')


function log(obj,num){
    num=num||Date.now();
    var url = './log-'+num+'.js';
    fs.writeFile(url, JSON.stringify(obj, null, '\t'), function (error) {
        if (error) {
        console.log('写入失败');
        } else {
        console.log('写入成功');
        }
    })
}
//新消息推送
function pusher1(content){
	 axios.get('http://wxmsg.dingliqc.com/demo/send/custom/UID_LET5BYBwfyMHAOizxrH4gkkhAvhv?content='+content)
    .then((response) => {
        //console.log('success')
        //log(response.data,'pusher1')
    })
    .catch((error) => {
        //console.log('error')
		log(error,'pusher1')
    });
}
//及时达
function pusher2(content){
	var title=encodeURI('无标题');
	content=encodeURI(content);
	 axios.get('http://push.ijingniu.cn/send?key=162b16fbbcb24da8bb59d04c2c69b0d4&head='+title+'&body='+content)
    .then((response) => {
       // console.log('success')
        
    })
    .catch((error) => {
       // console.log('error')
		log(error,'pusher2')
    });
}
//方糖
function pusher3(content){
	 axios.get('https://sc.ftqq.com/SCU83965Tefc5e457576b876e3f899cefcf36ff225e48c4ca599a5.send?text='+content)
    .then((response) => {
        //console.log('success')
    })
    .catch((error) => {
       // console.log('error')
		log(error,'pusher3')
    });
}
//钉钉
function sendDing(content,token){
    token = token || '3fc5ceb52ffc5e6c1d9f6b25df851013f9124faa0a1821e71c376d3da7884bc5';
    var config = {
      method: 'post',
      url: 'https://oapi.dingtalk.com/robot/send?access_token='+token,
      headers: { 
        'Content-Type': 'application/json'
      },
        data : JSON.stringify({
            "msgtype":"text",
            "text":{
              "content":new Date().toLocaleString()+`  消息：\n`+content
            }
        })
    };
    axios(config)
    .then(function (response) {
      //console.log(response.data);
    })
    .catch(function (error) {
      //console.log(error);
	  log(error,'sendDing')
    });
}

exports.log = log;
exports.pusher1 = pusher1;
exports.pusher2 = pusher2;
exports.pusher3 = pusher3;
exports.sendDing = sendDing;